package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.TransportTaskSub;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.TransportTaskSubDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2022-02-15 08:51:22
 */
public class TransportTaskSubConverts {

    private static Logger LOG = LoggerFactory.getLogger(TransportTaskSubConverts.class);

    public static List<TransportTaskSubDTO> convert(List<TransportTaskSub> transportTaskSubList) {
        if (CollectionUtils.isEmpty(transportTaskSubList)) {
            return Collections.EMPTY_LIST;
        }

        List<TransportTaskSubDTO> transportTaskSubListResult = Lists.newArrayList();
        for (TransportTaskSub transportTaskSub : transportTaskSubList) {
            transportTaskSubListResult.add(convert(transportTaskSub));
        }
        return transportTaskSubListResult;
    }

    public static TransportTaskSubDTO convert(TransportTaskSub transportTaskSub) {
        try {
            return BeanUtils.copy(transportTaskSub, TransportTaskSubDTO.class);
        } catch (Exception e) {
            LOG.error("TransportTaskSub到TransportTaskSubDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportTaskSub到TransportTaskSubDTO转换失败", e);
        }
    }

    public static TransportTaskSub convert(TransportTaskSubDTO transportTaskSubDTO) {
        try {
            return BeanUtils.copy(transportTaskSubDTO, TransportTaskSub.class);
        } catch (Exception e) {
            LOG.error("TransportTaskSubDTO到TransportTaskSub转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportTaskSubDTO到TransportTaskSub转换失败", e);
        }
    }
}